package com.hex.ds.hdtp.core.app.script.transfer.service.impl;

import com.hex.ds.hdtp.core.app.common.page.PageData;
import com.hex.ds.hdtp.core.app.common.page.converter.IPageAndPageDtoConverter;
import com.hex.ds.hdtp.core.app.common.page.dto.PageDto;
import com.hex.ds.hdtp.core.app.script.transfer.converter.IScriptVerifyLogConverter;
import com.hex.ds.hdtp.core.app.script.transfer.dto.request.ScriptVerifyLogRequestDto;
import com.hex.ds.hdtp.core.app.script.transfer.dto.response.ScriptVerifyLogResponseDto;
import com.hex.ds.hdtp.core.app.script.transfer.service.IScriptVerifyLogService;
import com.hex.ds.hdtp.core.inf.common.page.Page;
import com.hex.ds.hdtp.core.inf.script.transfer.po.ScriptVerifyLogPo;
import com.hex.ds.hdtp.core.inf.script.transfer.repository.IScriptVerifyLogRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor(onConstructor_ = {@Lazy, @Autowired})
public class ScriptVerifyLogService implements IScriptVerifyLogService {

    private final IScriptVerifyLogRepository scriptTestLogRepository;

    private final IPageAndPageDtoConverter pageAndPageDtoConverter;

    private final IScriptVerifyLogConverter converter;

    private ScriptVerifyLogPo toScriptConvertListTestPo(ScriptVerifyLogRequestDto scriptVerifyLogRequestDto){
        return converter.toScriptTestLogPo(scriptVerifyLogRequestDto);
    }

    @Override
    public PageData queryListByPage(ScriptVerifyLogRequestDto scriptVerifyLogRequestDto, PageDto pageDto) {
        final Page<ScriptVerifyLogPo> pageResult = scriptTestLogRepository.queryListByPage(
                toScriptConvertListTestPo(scriptVerifyLogRequestDto), pageAndPageDtoConverter.toPage(pageDto));
        List<ScriptVerifyLogResponseDto> responseDtos = converter.toScriptTestLogResponseDtoList(pageResult.getData());
        return new PageData(responseDtos, pageResult.getTotal());
    }

    @Override
    public void add(ScriptVerifyLogRequestDto scriptVerifyLogRequestDto) {
        if (scriptTestLogRepository.add(converter.toScriptTestLogPo(scriptVerifyLogRequestDto)) != 1){
           throw new RuntimeException("新增脚本验证记录失败");
        }
    }
}
